package com.gome.han.bigdata.hbase.apacheHBaseAPIs._002demo;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceExistException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.protobuf.generated.TableProtos;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.logging.Logger;

import java.io.IOException;

/**
 * @author Hanpeng
 * @date 2020/12/10 21:17
 * @description: HBase执行DDl操作
 */
public class TestDDL {
    private static Connection conn=null;
    private static Admin admin=null;
    static {
        try {
            //1,获取配置信息
            Configuration config = HBaseConfiguration.create();
            //conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem")
            //config.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");//无论设置与否 都不会报错
            config.set("hbase.zookeeper.quorum", "elkfour");
            //2,创建连接对象
            conn = ConnectionFactory.createConnection(config);
            //创建admin 对象
            admin = conn.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static Logger logger = Logger.getLogger(TestDDL.class.getName());
    public static void main(String[] args) throws IOException {
        //System.out.println(isTableExist("stu1"));
        //createTable("test4","user");
//        createTable("test100", "info1","info2");
        //createTable();
        //dropTable("test1");
        //creataNameSpace("kj");
        close();
    }

    /**
     * @param tableName
     * @return
     * @throws IOException
     * desc: 判断一个表是否存在
     */
    public  static  boolean isTableExist(String tableName) throws IOException {
        //Configuration config = HBaseConfiguration.create();
        //config.set("hbase.zookeeper.quorum", "elkfour");
        //Connection connection = ConnectionFactory.createConnection(config);
        //Admin admin = connection.getAdmin();
        //判断一个表是否存在
        //TableName 的对象不是new 出来的。
        TableName tableName1 = TableName.valueOf(tableName);
        boolean b = admin.tableExists(tableName1);
        //Table table = connection.getTable(TableName.valueOf("myLittleHBaseTable"));
       // System.out.println(table.);
        //configuration.set("hbase.zookeeper.quorum", "elkfour");
        //new Hbase
        //HBaseAdmin admin = new HBaseAdmin(configuration);
       /* admin.close();*/
        return b;
    }
    public static void createTable() throws IOException {
        /*Admin admin = conn.getAdmin();*/
        if(!admin.tableExists(TableName.valueOf("test2"))) {
            TableName tableName = TableName.valueOf("test2");
            //表描述器构造器
            TableDescriptorBuilder  tdb  =TableDescriptorBuilder.newBuilder(tableName)  ;
            //列族描述起构造器
            ColumnFamilyDescriptorBuilder cdb =  ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("user"));
            //获得列描述起
            ColumnFamilyDescriptor  cfd = cdb.build();
            //添加列族
            tdb.setColumnFamily(cfd);
            //获得表描述器
            TableDescriptor td = tdb.build();
            //创建表
            //admin.addColumnFamily(tableName, cfd); //给标添加列族
            admin.createTable(td);
        }else {
            System.out.println("表已存在");
        }
        //关闭链接
    }

    /**
     * @param tableName   表名
     * @param cf 列族 只有一个列族
     */
    public  static  void  createTable(String tableName,String cf) throws IOException {
        if(cf==null&&"".equals(cf)) {
            System.out.println("请输入列族信息");
            return ;
        }
        if(isTableExist(tableName)) {
            System.out.println(tableName+" 表已存在");
            return ;
        }
        TableName testTable= TableName.valueOf(tableName);
        //表描述器构造器
        TableDescriptorBuilder tdb  =TableDescriptorBuilder.newBuilder(testTable) ;
        //列族描述起构造器
        ColumnFamilyDescriptorBuilder cdb =  ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
        //获得列描述器
        ColumnFamilyDescriptor  cfd = cdb.build();
        //添加列族
        tdb.setColumnFamily(cfd);
        //获得表描述器
        TableDescriptor td = tdb.build();
        //创建表
        //admin.addColumnFamily(td, cfd); //给标添加列族
        admin.createTable(td);
    }
    /**
     * @param tableName   表名
     * @param cfs 有多个列族
     */
    public  static  void  createTable(String tableName,String... cfs) throws IOException {
        if(cfs==null&&cfs.length<=0) {
            System.out.println("请输入列族信息");
            return ;
        }
        if(isTableExist(tableName)) {
            System.out.println(tableName+" 表已存在");
            return ;
        }
        TableName testTable= TableName.valueOf(tableName);
        //表描述器构造器
        TableDescriptorBuilder tdb  =TableDescriptorBuilder.newBuilder(testTable) ;
        //列族描述起构造器
        ColumnFamilyDescriptorBuilder cdb =null;
        //获得列描述器
        ColumnFamilyDescriptor  cfd=null;
        for (String cf:cfs){
            cdb=ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
            cfd = cdb.build();
            tdb.setColumnFamily(cfd);
        }
        //获得表描述器
        TableDescriptor td = tdb.build();
        admin.createTable(td);
    }


    /**
     * @param tableName  表名称
     */
    //删除表
    public static  void  dropTable(String tableName) throws IOException {
        if(!isTableExist(tableName)) {
            System.out.println(tableName+"表不存在");
            return;
        }
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
    }

    /**
     * @param ns 命名空间
     * 创建一个命名空间
     */
    public static  void creataNameSpace(String ns)  {
        NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(ns);
        NamespaceDescriptor namespaceDescriptor = builder.build();
        try {
            admin.createNamespace(namespaceDescriptor);
        } catch (NamespaceExistException e1){
            System.out.println("mn,nm");
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    private  static  void  close() {
        if(admin!=null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
